package com.ruoyi.demo01;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: shunpeng.hu
 * @date: 2023/12/15 15:11
 */
public class Test012 {

    public static void main(String[] args) {
        try {
            //1.定义存储订单Order对象的集合
            List<Order> orderList = new ArrayList<>();
            //2.新建解析order.json文件输入流
            FileReader fileReader = new FileReader("ruoyi-javasepro\\src\\main\\resources\\order.json");
            //2.1新建解析缓冲输入流
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            String line;
            StringBuilder jsonOrder = new StringBuilder();
            //2.2按行读取字符
            while ((line = bufferedReader.readLine()) != null) {
                jsonOrder.append(line);
            }
            //3.解析JSON
            JSONObject orders = JSON.parseObject(jsonOrder.toString());
            //3.1获取JSON一级key
            JSONObject ordersObject = orders.getJSONObject("orders");
            //3.2获取JSON二级key
            JSONArray orderArray = ordersObject.getJSONArray("order");
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            //4.遍历订单json数组
            for (int i = 0; i < orderArray.size(); i++) {
                //4.1 获取JSON数组的每一级的对象
                JSONObject order = orderArray.getJSONObject(i);
                //4.2 封装订单对象
                Order newOrder = new Order();
                newOrder.setId(Long.valueOf(order.getString("id")));
                newOrder.setName(order.getString("name"));
                newOrder.setOrderTime(LocalDateTime.parse(order.getString("time"), formatter));
                newOrder.setPrice(Double.parseDouble(order.getString("price")));
                //4.3 订单对象添加到集合orderList
                orderList.add(newOrder);
            }
            System.out.println("=====订单内容如下：=========");
            for (Order order : orderList) {
                System.out.println(order);
            }
            //5.用Stream流找出今天之前的订单，并遍历输出
            System.out.println("=====今天之前的订单如下：=======");
            orderList.stream().filter(order ->
                    order.getOrderTime().isBefore(LocalDateTime.now())
            ).forEach(order -> System.out.println(order));
            //6.集合中价格最贵的订流单,这个订单的详细信息打印出来
            Order order = orderList.stream().max((order1, order2) ->
                    //比较规则 -1 最小的  1 最大的 0 相等
                    Double.compare(order1.getPrice(), order2.getPrice())
            ).get();
            System.out.println("======打印价格最贵的订流单详细信息如下：==========");
            System.out.println(order);
            //7.遍历集合中的每个订单，要求按照价格降序输出
            System.out.println("=======按照价格降序输出每个订单如下：=========");
            orderList.stream().sorted((order1, order2) ->
                    Double.compare(order2.getPrice(), order1.getPrice())
            ).forEach(curOrder -> System.out.println(curOrder));
            //8. 关闭缓冲流
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
